﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using WmsRunStart.Factory;
using WmsRunStart.Service;

namespace WmsRunStart
{
    public class UpdateChecker : IUpdateChecker
    {
        #region IUpdateChecker Members

        public List<UpdateList> GetUpdates()
        {
            List<UpdateList> list = WmsServiceFactory.GetServiceClient().GetAllAssemblyVersions();
            //开始和本地比较，如果版本不一致，则更新，同时修改本地文件
            var listString = new List<UpdateList>();
            string local = string.Format(@"{0}\localfile\", Application.StartupPath);

            if (!Directory.Exists(local))
            {
                Directory.CreateDirectory(local);
            }
            for (int i = 0; i < list.Count; i++)
            {
                string f = string.Format("{0}{1}.txt", local, list[i].TFileName);
                if (File.Exists(f))
                {
                    string t = File.ReadAllText(f);
                    if (int.Parse(t) < list[i].TVersion)
                    {
                        //File.WriteAllText(f,list[i].TVersion.ToString());
                        listString.Add(list[i]);
                    }
                }
                else
                {
                    //File.WriteAllText(f,list[i].TVersion.ToString());
                    listString.Add(list[i]);
                }
            }
            return listString;
        }

        public void DownloadFiles(List<UpdateList> files)
        {
            try
            {
                var strings = new List<string>();
                for (int idx = 0; idx < files.Count; idx++)
                {
                    strings.Add(files[idx].TFileName);
                }
                string dir = Application.StartupPath + "\\temp\\";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                List<byte[]> bytes = WmsServiceFactory.GetServiceClient().GetBytesByAssemblyName(strings);
                for (int idx = 0; idx < files.Count; idx++)
                {
                    File.WriteAllBytes(Application.StartupPath + "\\temp\\" + files[idx].TFileName, bytes[idx]);
                    string local = string.Format("{0}\\localfile\\", Application.StartupPath);
                    string f = string.Format("{0}{1}.txt", local, files[idx].TFileName);
                    File.WriteAllText(f, files[idx].TVersion.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void UpdateLocalFiles()
        {
            string temp = Application.StartupPath + "\\temp\\";
            if (Directory.Exists(temp))
            {
                string[] files = Directory.GetFiles(temp);
                foreach (string file in files)
                {
                    string fileName = file.Substring(file.LastIndexOf('\\') + 1);
                    File.Copy(file, Application.StartupPath + "\\" + fileName, true);
                    File.Delete(file);
                }
            }
        }

        #endregion
    }
}